package com.dddpeter.app.controller;

import com.dddpeter.app.util.IdUniquenessTest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 测试控制器
 * 用于测试ID生成器的唯一性
 */
@RestController
@RequestMapping("/api/test")
public class TestController {
    
    @Autowired
    private IdUniquenessTest idUniquenessTest;
    
    /**
     * 快速测试ID唯一性
     */
    @GetMapping("/id-uniqueness/quick")
    public Map<String, Object> quickTest() {
        Map<String, Object> result = new HashMap<>();
        try {
            idUniquenessTest.quickTest();
            result.put("success", true);
            result.put("message", "快速测试完成，请查看控制台输出");
        } catch (Exception e) {
            result.put("success", false);
            result.put("error", e.getMessage());
        }
        return result;
    }
    
    /**
     * 压力测试ID唯一性
     */
    @GetMapping("/id-uniqueness/stress")
    public Map<String, Object> stressTest() {
        Map<String, Object> result = new HashMap<>();
        try {
            idUniquenessTest.stressTest();
            result.put("success", true);
            result.put("message", "压力测试完成，请查看控制台输出");
        } catch (Exception e) {
            result.put("success", false);
            result.put("error", e.getMessage());
        }
        return result;
    }
    
    /**
     * 自定义测试参数
     */
    @PostMapping("/id-uniqueness/custom")
    public Map<String, Object> customTest(
            @RequestParam(defaultValue = "5") int threadCount,
            @RequestParam(defaultValue = "100") int idCountPerThread,
            @RequestParam(defaultValue = "article") String entityType) {
        
        Map<String, Object> result = new HashMap<>();
        try {
            if ("article".equals(entityType)) {
                idUniquenessTest.testArticleIdUniqueness(threadCount, idCountPerThread);
            } else if ("user".equals(entityType)) {
                idUniquenessTest.testUserIdUniqueness(threadCount, idCountPerThread);
            } else {
                result.put("success", false);
                result.put("error", "不支持的实体类型: " + entityType);
                return result;
            }
            
            result.put("success", true);
            result.put("message", "自定义测试完成，请查看控制台输出");
            result.put("threadCount", threadCount);
            result.put("idCountPerThread", idCountPerThread);
            result.put("entityType", entityType);
        } catch (Exception e) {
            result.put("success", false);
            result.put("error", e.getMessage());
        }
        return result;
    }
}
